﻿using Dapper;
using Entity;
using IRealize;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Realize
{
    public class EMR : IEMR
    {
        public async Task<int> delete_EMR(int id, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"delete from engage_major_release where mre_id={id}";
                return await con.ExecuteAsync(sql);
            }
        }

        public async Task<Engage_Major_Release> selectById_EMR(string id, string sqlserver)
        {
            Task<Engage_Major_Release?> task = Task<Engage_Major_Release>.Run(() =>
            {
                using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
                {
                    string sql = $@"select * from engage_major_release where mre_id={id}";
                    return sqlConnection.Query<Engage_Major_Release>(sql).FirstOrDefault();
                }
            });
            return await task;
        }

        public async Task<List<Engage_Major_Release>> select_EMR( string sqlserver)
        {
            Task<List<Engage_Major_Release>> task = Task<List<Engage_Major_Release>>.Run(async () =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {
                    string sql = "select * from engage_major_release";
                    return con.Query<Engage_Major_Release>(sql).ToList();
                }

            });
            return await task;
        }

        //public async Task<FenYe<Engage_Major_Release>> select_EMR(int CurrentPage, int PageSize,string sqlserver)
        //{
        //    Task<FenYe<Engage_Major_Release>> task = Task<FenYe<Engage_Major_Release>>.Run(async () =>
        //    {
        //        using (SqlConnection con = new SqlConnection(sqlserver))
        //        {
        //            string sql = $@"DECLARE @c INT
        //                        exec [dbo].[proc_FenYe] @pageSize, @keyName, @tabelName, @where, @currentPage, @rows= @c output;
        //                        SELECT @c;
        //                        ";
        //            var parameters = new DynamicParameters();
        //            parameters.Add("pageSize", dbType: DbType.Int32, direction: ParameterDirection.Input, value: PageSize);//输入参 
        //            parameters.Add("tabelName", dbType: DbType.String, direction: ParameterDirection.Input, value: "engage_major_release");//输入参
        //            parameters.Add("keyName", dbType: DbType.String, direction: ParameterDirection.Input, value: "mre_id");//输入参
        //            parameters.Add("currentPage", dbType: DbType.String, direction: ParameterDirection.Input, value: CurrentPage);//输入参
        //            parameters.Add("where", dbType: DbType.String, direction: ParameterDirection.Input, value: "1=1");//输入参
        //            parameters.Add("rows", dbType: DbType.Int32, direction: ParameterDirection.Output);//输出参
        //            List<Engage_Major_Release> li = con.Query<Engage_Major_Release>(sql).ToList();

        //            string sql2 = "SELECT COUNT(*) FROM dbo.engage_major_release";

        //            int num = con.Query<int>(sql2).FirstOrDefault();

        //            FenYe<Engage_Major_Release> fy = new FenYe<Engage_Major_Release>()
        //            {
        //                List = li,
        //                Rows = num
        //            };

        //            return fy;
        //        }

        //    });
        //    return await task;
        //}

        public async Task<int> update_EMR(int mre_id, short human_amount, string engage_type, DateTime deadline, string changer, DateTime change_time, string major_describe, string engage_required, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $@"update engage_major_release set engage_type='{engage_type}',human_amount='{human_amount}',deadline='{deadline}',changer='{changer}',change_time='{change_time}',major_describe='{major_describe}',engage_required='{engage_required}' where mre_id='{mre_id}'";
                return await con.ExecuteAsync(sql);
            }
        }

        
    }
}
